

const Controller = require('egg').Controller;

class SocialController extends Controller {
  // 发布说说
  async publish() {
    const ctx = this.ctx;
    ctx.validate({
      article: {
        type: 'string',
        allowEmpty: true,
      },
      pictures: 'array',
      tags: 'array',
    });
    ctx.success({
      data: await this.service.social.publish(ctx.request.body),
    });
  }

  async articleList() {
    const ctx = this.ctx;
    const {
      tags = '',
      nickname = '',
      start = 0,
      limit = 10,
    } = ctx.query;
    const parmas = {
      tags: tags ? tags.split(';') : [],
      nickname,
      start: start - 0,
      limit: limit - 0,
    };
    ctx.validate({
      tags: 'array',
      nickname: {
        type: 'string',
        allowEmpty: true,
      },
      start: 'int',
      limit: 'int',
    }, parmas);
    ctx.success({
      data: await this.service.social.getArticleList(parmas),
    });
  }
  async articleDetail() {
    const ctx = this.ctx;
    const {
      id,
    } = ctx.params;
    const parmas = {
      id: id - 0,
    };
    ctx.validate({
      id: 'int',
    }, parmas);
    ctx.success({
      data: await this.service.social.articleDetail(parmas),
    });
  }

  async comment() {
    const ctx = this.ctx;
    ctx.validate({
      comment: 'string',
      articleId: 'int',
    });
    ctx.success({
      data: await this.service.social.comment(ctx.request.body),
    });
  }
  async queryComments() {
    const ctx = this.ctx;
    const {
      articleId,
      start = 0,
      limit = 10,
    } = ctx.query;
    const parmas = {
      articleId: articleId - 0,
      start: start - 0,
      limit: limit - 0,
    };
    ctx.validate({
      articleId: 'int',
      start: 'int',
      limit: 'int',
    }, parmas);
    ctx.success({
      data: await this.service.social.queryComments(parmas),
    });
  }

  async like() {
    const ctx = this.ctx;
    ctx.validate({
      articleId: 'int',
    });
    ctx.success({
      data: await this.service.social.like(ctx.request.body),
    });
  }
}


module.exports = SocialController;
